Previous Book Contents Book Index Next

Inside Macintosh: AppleScript Language Guide / Part 2 - AppleScript Language Reference
Chapter 8 - Handlers / Using Subroutines


Checking the Classes of Subroutine Parameters

You cannot specify the class of a parameter in a subroutine definition. You can, however, get the value of the Class property of a parameter and check it to
see if the parameter belongs to the correct class. If it doesn't, you may be able
to coerce it with the As operator, or failing that, you can return an error.
(For information about coercing values, see Chapter 6, "Expressions." For information about returning errors, see "Try Statements," which begins on page 205.)

Here's an example of a subroutine that checks to see if its parameter is a real number or an integer:

on CentimeterConversion from x
   --make sure the parameter is a real number or an integer
   if class of x is contained by {integer, real}
      return x * 2.54
   else
      error "The parameter must be a real number or an integer"   end if
end CentimeterConversion

Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996